package training;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author liangzai
 * @Description:
 */
public class J25_7_9_Training {
    //重新安排会议得到最多空余时间 I
    public int maxFreeTime(int eventTime, int k, int[] startTime, int[] endTime) {
        // 先统计出空隙
        List<Integer> buffers = new ArrayList<>();
        buffers.add(startTime[0]);
        for (int i = 0; i < startTime.length - 1; i++) {
            buffers.add(startTime[i + 1] - endTime[i]);
        }
        buffers.add(eventTime - endTime[endTime.length - 1]);
        // 使用滑动窗口统计k个空隙的最大值
        int sum = 0;
        for (int i = 0; i <= k; i++) {
            sum += buffers.get(i);
        }
        int max = sum;
        for (int i = k + 1; i < buffers.size(); i++) {
            sum += buffers.get(i) - buffers.get(i - k - 1);
            max = Math.max(max, sum);
        }
        return max;
    }
}
